package com.cloud.DrugManagement.controller;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cloud.DrugManagement.pojo.DrugPrice;
import com.cloud.DrugManagement.pojo.Supplier;
import com.cloud.DrugManagement.service.DrugPriceService;
import com.cloud.DrugManagement.service.SupplierService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * @author zhozixiong
 * @date 2023/02/05
 */
@Slf4j
@Api(tags = "供应")
@CrossOrigin
@RestController
@RequiredArgsConstructor
public class SupplyController {

    private final SupplierService supplierService;
    private final DrugPriceService drugPriceService;

    @ApiOperation(value = "添加供应的商品")
    @PostMapping("/createDrugPrice")
    public Object createDrugPrice(@RequestBody JSONObject object) {
        int did = object.getInteger("did");
        int sid = object.getInteger("sid");
        Double retailPrice = object.getDouble("retailPrice");
        if(drugPriceService.insert(did, sid, retailPrice)) {
            return "ok";
        }else {
            return "full";
        }
    }

    @ApiOperation(value = "删除供应的商品")
    @GetMapping("/delDrugPrice/{did}/{sid}")
    public Object delDrugPrice(@PathVariable int did, @PathVariable int sid) {
        if(drugPriceService.delByDidAndSid(did, sid)) {
            return "ok";
        }else {
            return "full";
        }
    }

    @ApiOperation(value = "修改供应价格信息")
    @PostMapping("/updateDrugPrice")
    public Object updateDrugPrice(@RequestBody JSONObject object) {
        int did = object.getInteger("did");
        int sid = object.getInteger("sid");
        double price = object.getDouble("price");
        if(drugPriceService.updatePriceByDidAndSid(did, sid, price)) {
            return "ok";
        }else {
            return "full";
        }
    }

    @ApiOperation(value = "获取供应商信息和提供的商品")
    @GetMapping({"/getSupply/{pages}/{sid}", "/getSupply/{pages}/{sid}/{parameter}"})
    public Object getSupply(@PathVariable int pages, @PathVariable int sid, @PathVariable(required = false) Object parameter) {
        Supplier supplier = supplierService.searchAllBySid(sid);
        Page<DrugPrice> drugPricePage = drugPriceService.searchAllBySidOrDidOrDname(pages, sid, parameter);
        Map<String, Object> map = new HashMap<>(2);
        map.put("supplier", supplier);
        map.put("DrugPrice", drugPricePage);
        return map;
    }

    @ApiOperation(value = "分页获取供应商列表")
    @GetMapping({"/getSupplier/{pages}", "/getSupplier/{pages}/{parameter}"})
    public Object getSupplier(@PathVariable int pages, @PathVariable(required = false) Object parameter) {
        return supplierService.searchAllBySidOrSname(pages, parameter);
    }

    @ApiOperation(value = "添加供应商")
    @PostMapping("/createSupplier")
    public Object createSupplier(Supplier supplier) {
        if(supplierService.insert(supplier)) {
            return "ok";
        }else {
            return "full";
        }
    }

    @ApiOperation(value = "修改供应商信息")
    @PostMapping("/updateSupplier")
    public Object updateSupplier(Supplier supplier) {
        if(supplierService.updateSnameBySid(supplier.getSname(), supplier.getSid())) {
            return "ok";
        }else {
            return "full";
        }
    }

    @ApiOperation(value = "删除供应商")
    @GetMapping("/delSupplier/{sid}")
    public Object delSupplier(@PathVariable int sid) {
        if(supplierService.delBySid(sid)) {
            return "ok";
        }else {
            return "full";
        }
    }
}
